package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.ShowListAdapter;
import com.longcai.piaojia.conn.ShowListGet;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.Http;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/10.
 */
public class ShowActivity extends BaseActivity implements View.OnClickListener {

    //    @BoundView(R.id.show_tab_hlv) private HorizontalListView show_tab_hlv;
    @BoundView(R.id.show_lv)
    private PullToRefreshListView pullToRefreshListView;
    private ListView show_lv;
    //    private ShowTabListAdapter showTabListAdapter;
    private ShowListAdapter showListAdapter;
    //    private List<ShowTypeListGet.ShowTypeEntity> class_lists = new ArrayList<>();
    private List<ShowListGet.ShowEntity> show_lists = new ArrayList<>();
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    private ShowListGet.ShowListEntity showList;
    private Bundle bundle;

//    private ShowTypeListGet showTypeListGet = new ShowTypeListGet(new AsyCallBack<ShowTypeListGet.ShowTypeEntityList>(){
//
//        @Override
//        public void onStart(int type) throws Exception {
//            super.onStart(type);
//
//            Http.getInstance().show(ShowActivity.this);
//
//        }
//
//        @Override
//        public void onSuccess(String toast, int type, ShowTypeListGet.ShowTypeEntityList showTypeEntityList) throws Exception {
//
//            ShowTypeListGet.ShowTypeEntity showTypeEntity = new ShowTypeListGet.ShowTypeEntity();
//
//            showTypeEntity.id = "";
//
//            showTypeEntity.title = getResources().getString(R.string.show_activity_all_type_text);
//
//            class_lists.add(showTypeEntity);
//
//            class_lists.addAll(showTypeEntityList.showTypeEntityList);
//
//            if(class_lists.size()>0){
//
//                showListGet.region_id = BaseApplication.BasePreferences.getCityId();
//                showListGet.classid = class_lists.get(0).id;
//                showListGet.page = 1;
//                showListGet.execute(ShowActivity.this, false, 0);
//
//            }
//
//            showTabListAdapter.notifyDataSetChanged(class_lists);
//
//        }
//
//        @Override
//        public void onFail(String toast, int type) throws Exception {
//
//            Http.getInstance().dismiss();
//
//            UtilToast.show(ShowActivity.this, toast);
//
//        }
//    });

    private ShowListGet showListGet = new ShowListGet(new AsyCallBack<ShowListGet.ShowListEntity>() {

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);

            if (type == 0) {

                Http.getInstance().dismiss();

            } else {

                pullToRefreshListView.onPullDownRefreshComplete();
                pullToRefreshListView.onPullUpRefreshComplete();
                setLastUpdateTime();

            }

        }

        @Override
        public void onSuccess(String toast, int type, ShowListGet.ShowListEntity showListEntity) throws Exception {
            super.onSuccess(toast, type, showListEntity);

            showList = showListEntity;

            show_lists.addAll(showListEntity.list);

            showListAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(ShowActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshListView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        bundle = getIntent().getExtras();

        setBackTrue();

        setTitleName(bundle.getString("title", ""));

//        showTabListAdapter = new ShowTabListAdapter(ShowActivity.this, class_lists);
//        show_tab_hlv.setAdapter(showTabListAdapter);

        pullToRefreshListView.setPullRefreshEnabled(true);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setScrollLoadEnabled(false);
        show_lv = pullToRefreshListView.getRefreshableView();
        show_lv.setDivider(null);
        show_lv.setVerticalScrollBarEnabled(false);

        showListAdapter = new ShowListAdapter(ShowActivity.this, show_lists);
        show_lv.setAdapter(showListAdapter);

//        show_tab_hlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                if (showTabListAdapter.tabChooseMap.get(position) != true) {
//
//                    for (int i = 0; i < class_lists.size(); i++) {
//
//                        if (i == position) {
//
//                            showTabListAdapter.tabChooseMap.put(i, true);
//
//                        } else {
//
//                            showTabListAdapter.tabChooseMap.put(i, false);
//
//                        }
//
//                    }
//
//                    showTabListAdapter.notifyDataSetChanged();
//
//                    show_lists.clear();
//
//                    Http.getInstance().show(ShowActivity.this);
//
//                    showListGet.region_id = BaseApplication.BasePreferences.getCityId();
//                    showListGet.classid = class_lists.get(position).id;
//                    showListGet.page = 1;
//                    showListGet.execute(ShowActivity.this, false, 0);
//
//                }
//
//            }
//        });

        show_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(ShowActivity.this, GoodsInfoActivity.class).putExtra("id", show_lists.get(position).id));

            }
        });

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                show_lists.clear();
                showListGet.region_id = BaseApplication.BasePreferences.getCityId();
//                    showListGet.classid = class_lists.get(position).id;
                showListGet.page = 1;
                showListGet.execute(ShowActivity.this, false, 1);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (showList != null && showList.total != show_lists.size()) {

                    showListGet.page = showList.current_page + 1;

                    showListGet.execute(ShowActivity.this, false, 1);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();

                    pullToRefreshListView.onPullDownRefreshComplete();

                }

            }

        });

        setLastUpdateTime();

//        showTypeListGet.execute(ShowActivity.this, false);

        showListGet.region_id = BaseApplication.BasePreferences.getCityId();
        showListGet.classid = bundle.getString("id", "");
        showListGet.page = 1;
        showListGet.execute(ShowActivity.this, false, 0);

    }

    @Override
    public void onClick(View v) {

    }
}
